package com.ljy.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ljy.constant.ShopType;
import com.ljy.dto.LeaseDTO;
import com.ljy.dto.LeasePageDTO;
import com.ljy.entity.*;
import com.ljy.exception.ChangeStatusFailedException;
import com.ljy.exception.DeletionNotAllowedException;
import com.ljy.mapper.LeaseMapper;
import com.ljy.mapper.LeaseSpecMapper;
import com.ljy.mapper.ShopImageMapper;
import com.ljy.result.PageResult;
import com.ljy.service.LeaseService;
import com.ljy.vo.LeaseVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class LeaseServiceImpl implements LeaseService {
    @Autowired
    LeaseMapper leaseMapper;
    @Autowired
    ShopImageMapper shopImageMapper;
    @Autowired
    LeaseSpecMapper leaseSpecMapper;
    @Override
    @Transactional
    public void addLease(LeaseDTO leaseDTO) {
        Lease lease=new Lease();
        BeanUtils.copyProperties(leaseDTO,lease);
        lease.setCreateTime(LocalDateTime.now());
        lease.setUpdateTime(LocalDateTime.now());
        leaseMapper.insert(lease);
        Integer leaseId=lease.getId();
        //插入到规格表
        List<LeaseSpec> specs = lease.getSpecs();
        //time在前端解决
        if(specs!=null&&specs.size()!=0){
            leaseSpecMapper.insert(specs,leaseId);
        }
        //插入到图片表
        List<ShopImage> shopImages=lease.getShopImages();
        if(shopImages!=null&&shopImages.size()!=0){
            shopImageMapper.insertImage(shopImages,leaseId,ShopType.Lease_Type);
        }
    }

    @Override
    public PageResult<LeaseVO> selectPage(LeasePageDTO dto) {
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        List<LeaseVO> list=leaseMapper.selectPage(dto);
        Page<LeaseVO> p= (Page<LeaseVO>) list;
        return new PageResult<>(p.getTotal(),p.getResult());
    }

    @Override
    @Transactional
    public Lease selectById(Integer id) {
        Lease lease=leaseMapper.selectById(id);
        List<ShopImage> shopImages=shopImageMapper.selectByProductId(id,ShopType.Lease_Type);
        lease.setShopImages(shopImages);
        List<LeaseSpec>specs=leaseSpecMapper.selectByLeaseId(id);
        lease.setSpecs(specs);
        return lease;
    }
    @Override
    @Transactional
    public void modifyLease(LeaseDTO dto){
        Integer leaseId= dto.getId();
        Lease lease=new Lease();
        BeanUtils.copyProperties(dto,lease);
        lease.setUpdateTime(LocalDateTime.now());
        leaseMapper.updateLease(lease);
        //删除图片和规格
        shopImageMapper.deleteByProductId(leaseId,ShopType.Lease_Type);
        leaseSpecMapper.deleteByLeaseId(leaseId);
        //再添加图片和规格
        if(dto.getSpecs()!=null&&dto.getSpecs().size()!=0)
            leaseSpecMapper.insert(dto.getSpecs(),leaseId);
        //插入内容图片表设置图片type为商品
        List<ShopImage>shopImages=dto.getShopImages();
        if(shopImages!=null&&shopImages.size()!=0){
            shopImageMapper.insertImage(shopImages,leaseId,ShopType.Lease_Type);
        }

    }

    @Override
    @Transactional
    public void batchDelete(List<Integer> ids){
        if(ids==null || ids.size()<1){
            throw new DeletionNotAllowedException("删除的商品不能为空");
        }
        leaseMapper.batchDelete(ids);
        shopImageMapper.batchDelete(ids,ShopType.Lease_Type);
        leaseSpecMapper.batchDelete(ids);
    }
    @Override
    public void changeStatus(Short status, Integer id) {
        //如果货不够，不能启用
        Lease lease=leaseMapper.selectById(id);
        if(lease.getStock()==0&&status==1){
            throw new ChangeStatusFailedException("该租赁商品的库存不足不能启用");
        }
        leaseMapper.changeStatus(status,id);

    }

    @Override
    public void rollImageSubmit(List<RollImage> dto) {
        leaseMapper.rollImageDelete();
        leaseMapper.rollImageSubmit(dto);
    }

    @Override
    public List<RollImage> getRollImage() {
        return leaseMapper.rollImageSelect();
    }


}
